package com.example.tongue.ui.Online.Body;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tongue.ui.Online.Body.utils.ScoreCalculationEight;
import com.example.tongue.R;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class QiDeficiency extends Activity implements View.OnClickListener {
    private Button next2,whetherQiD;
    private RadioGroup tiredQd,shortnessOfBreath,flustered,dizziness,cough,likeQuiet,powerless,sweat;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qideficiency);
        //控件绑定资源
        next2=findViewById(R.id.Next2);

                tiredQd=findViewById(R.id.TiredQd);
                shortnessOfBreath=findViewById(R.id.ShortnessOfBreath);
                flustered=findViewById(R.id.Flustered);
                dizziness=findViewById(R.id.Dizziness);
                cough=findViewById(R.id.Cough);
                likeQuiet=findViewById(R.id.LikeQuiet);
                powerless=findViewById(R.id.Powerless);
                sweat=findViewById(R.id.Sweat);

        //注册监听器
        next2.setOnClickListener(this);

                tiredQd.setOnClickListener(this);
                shortnessOfBreath.setOnClickListener(this);
                flustered.setOnClickListener(this);
                dizziness.setOnClickListener(this);
                cough.setOnClickListener(this);
                likeQuiet.setOnClickListener(this);
                powerless.setOnClickListener(this);
                sweat.setOnClickListener(this);


    }
    @Override
    public void onClick(View v) {
            switch (v.getId()){

                case R.id.Next2:{
                    //计算结果,把结果写入缓存
                    QIDResult();
//                    Toast.makeText(QiDeficiency.this, "进入阳虚质判断", Toast.LENGTH_SHORT).show();

                    startActivity(new Intent(QiDeficiency.this,YangDeficiency.class));
                }
                break;
            }


    }

    public void QIDResult(){

            int tiredQdid=tiredQd.getCheckedRadioButtonId();
            RadioButton tiredQdidbtn= findViewById(tiredQdid);
            String tiredQdidfin=tiredQdidbtn.getText().toString();

            int  shortnessOfBreathid=  shortnessOfBreath.getCheckedRadioButtonId();
            RadioButton shortnessOfBreathbtn= findViewById( shortnessOfBreathid);
            String shortnessOfBreathfin=shortnessOfBreathbtn.getText().toString();

            int flusteredid=flustered.getCheckedRadioButtonId();
            RadioButton flusteredbtn= findViewById(flusteredid);
            String  flusteredfin= flusteredbtn.getText().toString();

            int dizzinessid=dizziness.getCheckedRadioButtonId();
            RadioButton dizzinessbtn= findViewById(dizzinessid);
            String dizzinessfin=dizzinessbtn.getText().toString();

            int coughid=cough.getCheckedRadioButtonId();
            RadioButton coughbtn= findViewById(coughid);
            String coughfin=coughbtn.getText().toString();

            int likeQuietid=likeQuiet.getCheckedRadioButtonId();
            RadioButton likeQuietbtn= findViewById(likeQuietid);
            String likeQuietfin=likeQuietbtn.getText().toString();

            int powerlessid= powerless.getCheckedRadioButtonId();
            RadioButton powerlessbtn= findViewById(powerlessid);
            String powerlessfin=powerlessbtn.getText().toString();

            int  sweatid= sweat.getCheckedRadioButtonId();
            RadioButton sweatidbtn= findViewById(sweatid);
            String sweatidfin=sweatidbtn.getText().toString();

            Map<String,Integer> tiredQdMap=new HashMap<String,Integer>();
            tiredQdMap.put("没有",1);
            tiredQdMap.put("很少",2);
            tiredQdMap.put("有时",3);
            tiredQdMap.put("常常",4);
            tiredQdMap.put("经常",5);

            Map<String,Integer>   shortnessOfBreathMap=new HashMap<String,Integer>();
            shortnessOfBreathMap.put("没有",1);
            shortnessOfBreathMap.put("很少",2);
            shortnessOfBreathMap.put("有时",3);
            shortnessOfBreathMap.put("常常",4);
            shortnessOfBreathMap.put("经常",5);

            Map<String,Integer> flusteredMap=new HashMap<String,Integer>();
            flusteredMap.put("没有",1);
            flusteredMap.put("很少",2);
            flusteredMap.put("有时",3);
            flusteredMap.put("常常",4);
            flusteredMap.put("经常",5);

            Map<String,Integer>  dizzinessMap=new HashMap<String,Integer>();
            dizzinessMap.put("没有",1);
            dizzinessMap.put("很少",2);
            dizzinessMap.put("有时",3);
            dizzinessMap.put("常常",4);
            dizzinessMap.put("经常",5);

            Map<String,Integer> coughMap=new HashMap<String,Integer>();
            coughMap.put("没有",1);
            coughMap.put("很少",2);
            coughMap.put("有时",3);
            coughMap.put("常常",4);
            coughMap.put("经常",5);

            Map<String,Integer> likeQuietMap=new HashMap<String,Integer>();
            likeQuietMap.put("没有",1);
            likeQuietMap.put("很少",2);
            likeQuietMap.put("有时",3);
            likeQuietMap.put("常常",4);
            likeQuietMap.put("经常",5);

            Map<String,Integer> powerlessMap=new HashMap<String,Integer>();
            powerlessMap.put("没有",1);
            powerlessMap.put("很少",2);
            powerlessMap.put("有时",3);
            powerlessMap.put("常常",4);
            powerlessMap.put("经常",5);



            Map<String,Integer> sweatMap=new HashMap<String,Integer>();
            sweatMap.put("没有",1);
            sweatMap.put("很少",2);
            sweatMap.put("有时",3);
            sweatMap.put("常常",4);
            sweatMap.put("经常",5);

            Integer qResult =tiredQdMap.get(tiredQdidfin)+
                    shortnessOfBreathMap.get(shortnessOfBreathfin) +
                    flusteredMap.get(flusteredfin) +
                    dizzinessMap.get(dizzinessfin)+
                    coughMap.get(coughfin)+
                    likeQuietMap.get(likeQuietfin)+
                    powerlessMap.get(powerlessfin)+
                    sweatMap.get(sweatidfin)
                    ;
        ScoreCalculationEight scoreCalculationEight=new ScoreCalculationEight(qResult);
        String ConversionScore=scoreCalculationEight.getResult();

                TextView qIDresult=(TextView)findViewById(R.id.QiDResult);
                qIDresult.setText("气虚质结果:"+ConversionScore);


        File dataFileDir=new File(getExternalCacheDir(),"PhysiqueDataFile");
        FileWriter writer=null;
        try {
            //开启写入流
            writer=new FileWriter(dataFileDir+"/PhysiqueData.txt",true);
            writer.write(ConversionScore+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println("数据文件已生成");
            }
        }


        }


}
